package com.envision.quartz;

import com.envision.eeop.api.domain.MdmChildObjects;
import com.envision.eeop.api.domain.MdmObject;
import com.envision.mdm.service.MdmService;
import com.envision.mdmidinfo.dao.MdmidInfoDao;
import com.envision.mdmidinfo.domain.MdmidInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 * <strong>Title : FomateEquipmentInfoJob.java<br>
 * </strong> <strong>Description :对场站下的设备进行分类 分楼层等信息 </strong><br>
 * <strong>Create on : 2018年07月10日<br>
 * <p>
 *
 * @author karrychang<br>
 * @version <strong>1.0.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人		修改日期		修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
public class FomateMdmidInfoJob {

    private static final Logger logger = LoggerFactory.getLogger(FomateMdmidInfoJob.class);
    @Autowired
    private MdmService mdmService;
    @Autowired
    private MdmidInfoDao mdmidInfoDao;

    private String siteIds;


    /** 
    *************************************************************************
     * FomateMdmidInfoJob.java——execute<br>
     * Author: karrychang<br>
     * Date: 2018/7/17<br>
     * Description:格式化主数据信息<br>
     * Used in：<br>
     * @param 
     * @return
     *************************************************************************
     */
    public void execute() {
        String[] sites=siteIds.split(",");
        Map<String,MdmChildObjects> data=mdmService.getObject(Arrays.asList(sites),null,null);
        if(data==null||data.isEmpty()){
            logger.warn("sites ({}) getObject fail ",Arrays.asList(sites));
        }
        List<MdmidInfo> mdmidInfos=new ArrayList<>();
        for (String siteid:sites){
            MdmChildObjects mdmChildObjects=data.get(siteid);
            String timezone=mdmChildObjects.getAttributes().get("timezone");
            List<MdmObject> list=mdmChildObjects.getMdmObjectList().get("210");
            for (MdmObject mdmObject:list){
                MdmidInfo mdmidInfo=new MdmidInfo();
                mdmidInfo.setMdmid(mdmObject.getMdmID());
                mdmidInfo.setBuliding(mdmObject.getAttributes().get("building"));
                mdmidInfo.setTimezone(timezone);
                mdmidInfo.setSys(mdmObject.getAttributes().get("system"));
                mdmidInfo.setFloor(mdmObject.getAttributes().get("floor"));
                mdmidInfo.setName(mdmObject.getAttributes().get("name"));
                mdmidInfo.setUpdatetime(new Date());
                mdmidInfo.setSiteid(siteid);
                mdmidInfos.add(mdmidInfo);
            }
        }
        mdmidInfoDao.truncateTable();
        int i=mdmidInfoDao.insertMdmidInfoList(mdmidInfos);
        logger.info("add mdmininfo size ({})" ,i);
    }

    public String getSiteIds() {
        return siteIds;
    }

    public void setSiteIds(String siteIds) {
        this.siteIds = siteIds;
    }

}
